package com.imbalancegames.quran4droid;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.Spanned;

public class util {
	public static String [] surah_name;// = getSurahName();
	public static int [] ayat_count;// = getAyatCount();
	public static int [] quran_res;
	/*
	public static void main (String []args) {
		Vector<String> v = search_surah(1);
		for (int i=0; i<v.size(); i++) {
			System.out.println(i + ": " + v.get(i)  );
			try {
				java.lang.Thread.sleep(10);
			} catch (Exception e) {}
		}
		
		
	}
	*/
	

	
	public util () {
		surah_name = getSurahName();
		ayat_count = getAyatCount();
		quran_res = setQuranRes();
	}
	
	
	/*
	public static String search_ayat (int surah_id, int ayat_id) {
		return "123";
	}
	*/
	
	public static SpannableStringBuilder search_surah (Resources r, int surah_id, int current_theme) {
		int ayat_index = 1;
		//String retval = "";
		//StringBuilder sb = new StringBuilder();
		SpannableStringBuilder  ss = new SpannableStringBuilder ();
		try {
			//FileInputStream fstream = new FileInputStream("/sdcard/quran_english.txt");
			InputStream fstream = r.openRawResource((int) quran_res[surah_id-1]);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			int len = 0;
			int ayat_index_len = 1;
			int num_color = 0xFF98AFC7;
			
			//Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				
				//retval = retval + ayat_index + " " + strLine + "\n";
				len = ss.length();
				ss.append(Integer.toString(ayat_index));
				ss.append(" ");
				ss.append(strLine);
				ss.append("\n");
				
				if (ayat_index < 10) {
					ayat_index_len = 1;
				} else if (ayat_index > 10 && ayat_index < 100) {
					ayat_index_len = 2;
				} else {
					ayat_index_len = 3;
				}
				
				
				if (current_theme == 0) {
					num_color = 0xFF98AFC7;		// black
				} else if (current_theme == 1) {
					num_color = 0xFF000000;		// white
				} else if (current_theme == 2) {
					num_color = 0xFF663333;		// gray
				} else if (current_theme == 3) {
					num_color = 0xFF660000;		// yellow
				} else if (current_theme == 4) {
					num_color = 0xFFFF9900;		// blue
				}
				
				ss.setSpan(new ForegroundColorSpan(num_color), len, len+ayat_index_len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				ss.setSpan(new StyleSpan(Typeface.BOLD), len, len+ayat_index_len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				
				/*
				sb.append("<font color=\"#98AFC7\">");
				sb.append(ayat_index); 
				sb.append("</font> ");
				sb.append(strLine);
				sb.append("<br>");
				*/
				
				ayat_index++;
				
				
				
			}

			in.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		} finally {}
		
		
		//retval = ss.toString();
		//return retval;
		return ss;
	}
	
	
	public static int [] setQuranRes () {
		int [] quranRes = {
			R.raw.quran_part1, 
			R.raw.quran_part2, 
			R.raw.quran_part3, 
			R.raw.quran_part4, 
			R.raw.quran_part5, 
			R.raw.quran_part6, 
			R.raw.quran_part7, 
			R.raw.quran_part8, 
			R.raw.quran_part9, 
			R.raw.quran_part10, 
			R.raw.quran_part11, 
			R.raw.quran_part12, 
			R.raw.quran_part13, 
			R.raw.quran_part14, 
			R.raw.quran_part15, 
			R.raw.quran_part16, 
			R.raw.quran_part17, 
			R.raw.quran_part18, 
			R.raw.quran_part19, 
			R.raw.quran_part20, 
			R.raw.quran_part21, 
			R.raw.quran_part22, 
			R.raw.quran_part23, 
			R.raw.quran_part24, 
			R.raw.quran_part25, 
			R.raw.quran_part26, 
			R.raw.quran_part27, 
			R.raw.quran_part28, 
			R.raw.quran_part29, 
			R.raw.quran_part30, 
			R.raw.quran_part31, 
			R.raw.quran_part32, 
			R.raw.quran_part33, 
			R.raw.quran_part34, 
			R.raw.quran_part35, 
			R.raw.quran_part36, 
			R.raw.quran_part37, 
			R.raw.quran_part38, 
			R.raw.quran_part39, 
			R.raw.quran_part40, 
			R.raw.quran_part41, 
			R.raw.quran_part42, 
			R.raw.quran_part43, 
			R.raw.quran_part44, 
			R.raw.quran_part45, 
			R.raw.quran_part46, 
			R.raw.quran_part47, 
			R.raw.quran_part48, 
			R.raw.quran_part49, 
			R.raw.quran_part50, 
			R.raw.quran_part51, 
			R.raw.quran_part52, 
			R.raw.quran_part53, 
			R.raw.quran_part54, 
			R.raw.quran_part55, 
			R.raw.quran_part56, 
			R.raw.quran_part57, 
			R.raw.quran_part58, 
			R.raw.quran_part59, 
			R.raw.quran_part60, 
			R.raw.quran_part61, 
			R.raw.quran_part62, 
			R.raw.quran_part63, 
			R.raw.quran_part64, 
			R.raw.quran_part65, 
			R.raw.quran_part66, 
			R.raw.quran_part67, 
			R.raw.quran_part68, 
			R.raw.quran_part69, 
			R.raw.quran_part70, 
			R.raw.quran_part71, 
			R.raw.quran_part72, 
			R.raw.quran_part73, 
			R.raw.quran_part74, 
			R.raw.quran_part75, 
			R.raw.quran_part76, 
			R.raw.quran_part77, 
			R.raw.quran_part78, 
			R.raw.quran_part79, 
			R.raw.quran_part80, 
			R.raw.quran_part81, 
			R.raw.quran_part82, 
			R.raw.quran_part83, 
			R.raw.quran_part84, 
			R.raw.quran_part85, 
			R.raw.quran_part86, 
			R.raw.quran_part87, 
			R.raw.quran_part88, 
			R.raw.quran_part89, 
			R.raw.quran_part90, 
			R.raw.quran_part91, 
			R.raw.quran_part92, 
			R.raw.quran_part93, 
			R.raw.quran_part94, 
			R.raw.quran_part95, 
			R.raw.quran_part96, 
			R.raw.quran_part97, 
			R.raw.quran_part98, 
			R.raw.quran_part99, 
			R.raw.quran_part100, 
			R.raw.quran_part101, 
			R.raw.quran_part102, 
			R.raw.quran_part103, 
			R.raw.quran_part104, 
			R.raw.quran_part105, 
			R.raw.quran_part106, 
			R.raw.quran_part107, 
			R.raw.quran_part108, 
			R.raw.quran_part109, 
			R.raw.quran_part110, 
			R.raw.quran_part111, 
			R.raw.quran_part112, 
			R.raw.quran_part113, 
			R.raw.quran_part114
		};
		return quranRes;
	}
	
	
	public static int [] getAyatCount () {
		int [] ayat_count = {7,286,200,176,120,165,206,75,129,109,123,111,43,52,99,128,111,110,98,
			135,112,78,118,64,77,227,93,88,69,60,34,30,73,54,45,83,182,88,75,85,54,
			53,89,59,37,35,38,29,18,45,60,49,62,55,78,96,29,22,24,13,14,11,11,18,12,
			12,30,52,52,44,28,28,20,56,40,31,50,40,46,42,29,19,36,25,22,17,19,26,30,
			20,15,21,11,8,8,19,5,8,8,11,11,8,3,9,5,4,7,3,6,3,5,4,5,6};
		return ayat_count;
	}
	
	public static String [] getSurahName () {
		String [] retval = { 
			"Al-Fatiha",
			"Al-Baqarah",
			"Al-'Imran",
			"An-Nisa",
			"Al-Ma'idah",
			"Al-An'am",
			"Al-A'raf",
			"Al-Anfal",
			"At-Taubah",
			"Yunus",
			"Hud",
			"Yusuf",
			"Ar-Ra'd",
			"Ibrahim",
			"Al-Hijr",
			"An-Nahl",
			"Al-Isra'",
			"Al-Kahf",
			"Maryam",
			"Ta-Ha",
			"Al-Anbiya",
			"Al-Hajj",
			"Al-Mu'minun",
			"An-Nur",
			"Al-Furqan",
			"Ash-Shu'ara",
			"An-Naml",
			"Al-Qasas",
			"Al-Ankabut",
			"Ar-Rum",
			"Luqman",
			"As-Sajdah",
			"Al-Ahzab",
			"Saba",
			"Fatir",
			"Ya-Sin",
			"As-Saffaat",
			"Saad",
			"Az-Zumar",
			"Ghafir",
			"Fussilat",
			"Ash-Shura",
			"Az-Zukhruf",
			"Ad-Dukhan",
			"Al-Jathiyah",
			"Al-Ahqaf",
			"Muhammad",
			"Al-Fath",
			"Al-Hujuraat",
			"Qaf",
			"Adh-Dhariyat",
			"At-Tur",
			"An-Najm",
			"Al-Qamar",
			"Ar-Rahman",
			"Al-Waqi'ah",
			"Al-Hadid",
			"Al-Mujadilah",
			"Al-Hashr",
			"Al-Mumtahana",
			"As-Saff",
			"Al-Jumu'ah",
			"Al-Munafiqun",
			"At-Taghabun",
			"At-Talaaq",
			"At-Tahrim",
			"Al-Mulk",
			"Al-Qalam",
			"Al-Haqqah",
			"Al-Ma'arij",
			"Nuh",
			"Al-Jinn",
			"Al-Muzzammil",
			"Al-Muddathir",
			"Al-Qiyamah",
			"Al-Insan",
			"Al-Mursalaat",
			"An-Naba",
			"An-Nazi'aat",
			"'Abasa",
			"At-Takwir",
			"Al-Infitaar",
			"Al-Mutaffifi",
			"Al-Inshiqaq",
			"Al-Buruj",
			"At-Tariq",
			"Al-A'la",
			"Al-Ghaashiya",
			"Al-Fajr",
			"Al-Balad",
			"Ash-Shams",
			"Al-Lail",
			"Ad-Duha",
			"Ash-Sharh",
			"At-Tin",
			"Al-'Alaq",
			"Al-Qadr",
			"Al-Baiyyinah",
			"Az-Zalzalah",
			"Al-'Aadiyaat",
			"Al-Qari'ah",
			"At-Takaathur",
			"Al-'Asr",
			"Al-Humazah",
			"Al-Fil",
			"Al-Quraish",
			"Al-Maa'un",
			"Al-Kauthar",
			"Al-Kaafirun",
			"An-Nasr",
			"Al-Masad",
			"Al-Ikhlaas",
			"Al-Falaq",
			"An-Naas"
			};
		return retval;
	}
    
    
}



